package me.gacl.websocket.JSR356.example;

import javax.websocket.Session;

/**
 * 数据推送工具类
 * Created by 孤傲苍狼 on 2016/2/18.
 */
public class PushDataUtil {

    /**
     * 推送数据到客户端,在其他业务方法中调用
     * 异步的方式
     * @param clientId
     * @param message
     */
    public static void pushData(String clientId, String message) {
        if (SessionUtils.hasConnection(clientId)) {
            try {
                //异步推送
                SessionUtils.get(clientId).getAsyncRemote().sendText(message);
                //SessionUtils.get(clientName).getBasicRemote().sendText(message);
            } catch (Exception e) {
                e.printStackTrace();
                System.out.println("数据推送出错，原因是："+e);
            }
        } else {
            throw new NullPointerException(clientId +"Connection does not exist");
        }

    }
}
